package p2pcdnsim.xml;

import java.util.Iterator;
import org.jdom.Document;
import org.jdom.Element;

public class VideoObjectsXMLHandler implements XMLHandler {

	private Document doc;
	private String bitRate;

	public VideoObjectsXMLHandler(Document doc) {
		this.doc = doc;
	}

	public VideoObjectsXMLHandler(Document doc, String bitRate) {
		this.bitRate = bitRate;
		this.doc = doc;
	}

	@SuppressWarnings("unchecked")
	public Iterator<Element> iterator() {
		return new VideoIterator(this.doc.getRootElement().getChildren("Object").iterator());
	}
	
	private class VideoIterator implements Iterator<Element> {
		
		Iterator<Element> it;
		
		VideoIterator(Iterator<Element> it) {
			this.it = it;
		}

		@Override
		public boolean hasNext() {
			return it.hasNext();
		}

		@Override
		public Element next() {
			Element element = it.next();
			if(element.getChild("BitRate").getText() == null || element.getChild("BitRate").getText().equals("null"))
				element.getChild("BitRate").setText(bitRate);
			return element;
		}

		@Override
		public void remove() {
			it.remove();
		}
	}

}
